package cn.com.dhcc.fornew.pet.controller;



import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.log.Log;
import cn.com.dhcc.app.core.component.pager.Pager;
import cn.com.dhcc.app.core.component.pager.PagerData;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.fornew.pet.service.DogService;
import cn.com.dhcc.fornew.pet.vo.Dog;

/**
 * 
 * @日期：2017-6-1下午10:47:31
 * @作者： 杨贵隆
 */
@Controller
@RequestMapping("/pet")
public class DogController extends BaseController {

	private  static Logger logger = Log.getLogger(LogType.SYSINNER);


	@Autowired
	private DogService dogService;
	
	/**
	 *  展示列表
	 * @param model
	 * @param pager
	 * @param name
	 * @param orderName
	 * @param order
	 * @return
	 */
	@RequestMapping("/dog_list")
	public String list(Model model, Pager pager, String name, String orderName, String order) {
		
		
		try {
			WhereCondition searchData = new WhereCondition();
			if (StrUtil.isNotBlank(name)) {
				searchData.where().like("name", "%"+name+"%");
			}
			if (StrUtil.isNotBlank(orderName)) {
				searchData.orderBy(orderName + " " + order);
			} else {
				searchData.orderBy("created  desc");
			}
			PagerData<Dog> datas = dogService.list(pager, searchData);
			model.addAttribute(DATA_KEY, datas.getDatas());
			setTotoal(model, datas.getTotal());
			return "pet/dog_list";
		} catch (Exception e) {
			
			return toAlertPage(model, e.getMessage());
		}
	}
	
	/**
	 * 查看
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/dog_get")
	public String get(Model model,String id) {
		try {
			Dog vo = dogService.get(id);
			model.addAttribute(VO_KEY, vo);
		} catch (Exception e) {
			logger.error("获取业务域信息失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "pet/dog_get";
	}
	
	
	/**
	 * 更新
	 * @param model
	 * @param id
	 * @return
	 */
	
	@RequestMapping("/dog_update")
	public String update(Model model, String id) {
		try {	
			
			Dog vo = dogService.get(id);
			model.addAttribute(VO_KEY, vo);
			return "pet/dog_update";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	
	/**
	 * 批量更新
	 * @param model
	 * @param vo
	 * @return
	 */
	@RequestMapping(value = "/update_dog", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(Model model, Dog vo) {
		String msg = "";
		try {
			
			vo.setCreated(new Date());
			dogService.update(vo);
			msg = "更新成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "更新失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	
	/**
	 * 增加
	 * @param model
	 * @return
	 */
	@RequestMapping("/dog_add")
	public String addNode(Model model) {
		try {
			return "pet/dog_add";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	
	/**
	 * 批量增加
	 * @param model
	 * @param vo
	 * @return
	 */
	
	@RequestMapping(value = "/add_dog", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addNode(Model model, Dog vo) {
		String msg = "";
		try {
			vo.setId(UUIDGenerator.getUUID());
			vo.setCreated(new Date());
			dogService.insert(vo);
			msg = "添加成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "添加失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 根据ID删除记录
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/dog_delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(String id) {
		try {
				dogService.delete(id);
			return jsonSuccessResult("删除成功");
		} catch (Exception e) {
			logger.error("删除失败", e);
			return jsonFailResult("删除失败");
		}
	}
	
	/**
	 *校验昵称是否被使用
	 * @param name
	 * @param oldValue
	 * @return
	 */
	
	@RequestMapping("/dog_check_name")
	@ResponseBody
	public String checkName(String name,String oldValue) {
		
		try {
			if (dogService.isPropertyUnique("name", name, oldValue)) {
				return FORM_CHECK_SUCCESS;
			} else {
				return FORM_CHECK_FAIL;
			}
		} catch (Exception e) {
			return FORM_CHECK_FAIL;
		}
	 }
	}
